package org.elanfox.intellicommunity.intellicommunityadmin.controller.equipment;

import org.elanfox.intellicommunity.biz.dto.condition.BaseStationCondition;
import org.elanfox.intellicommunity.biz.dto.condition.CardCondition;
import org.elanfox.intellicommunity.biz.dto.condition.LocatorCondition;
import org.elanfox.intellicommunity.biz.dto.equipment.BaseStationSaveDTO;
import org.elanfox.intellicommunity.biz.dto.equipment.CardSaveDTO;
import org.elanfox.intellicommunity.biz.dto.equipment.ConcealPointSaveDTO;
import org.elanfox.intellicommunity.biz.dto.equipment.LocatorSaveDTO;
import org.elanfox.intellicommunity.biz.entity.equipment.BaseStationPO;
import org.elanfox.intellicommunity.biz.entity.equipment.CardPO;
import org.elanfox.intellicommunity.biz.entity.equipment.ConcealPointPO;
import org.elanfox.intellicommunity.biz.entity.equipment.LocatorPO;
import org.elanfox.intellicommunity.biz.service.core.equipment.*;
import org.elanfox.intellicommunity.biz.service.core.resident.WorkerService;
import org.elanfox.intellicommunity.biz.vo.ConcealPointVO;
import org.elanfox.intellicommunity.biz.vo.LocatorVO;
import org.elanfox.intellicommunity.common.base.ApiResult;
import org.elanfox.intellicommunity.common.base.ResultCodeEnum;
import org.elanfox.intellicommunity.common.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author loutianxi
 * @date 2019-12-08
 */
@Controller
@RequestMapping("/equipment")
public class EquipmentController {
    @Autowired
    private CardService cardService;
    @Autowired
    private BaseStationService baseStationService;
    @Autowired
    private LocatorService locatorService;
    @Autowired
    private ConcealPointService concealPointService;
    @Autowired
    private WorkerService workerStructureService;
    @Autowired
    private LocatorAreaService locatorAreaService;

    /**
     * 跳转到卡片列表页面
     *
     * @return
     */
    @RequestMapping("/card/list")
    public String cardListPage(@ModelAttribute("condition") CardCondition cardCondition,
                               @RequestParam(required = false, defaultValue = "1") Integer pageIndex, Model model) {
        Page<CardPO> page = cardService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, cardCondition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", page.getTotalElements());
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("cards", page.getContent());
        model.addAttribute("status", cardCondition.getStatus());
        model.addAttribute("voltageStatus", cardCondition.getVoltageStatus());
        return "equipment/card_list";
    }

    /**
     * 新增/修改卡片
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/ajax/save/card", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveCard(CardSaveDTO dto) {
        CardPO cardPO = cardService.save(dto);
        if (cardPO == null) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, dto);
    }

    /**
     * 修改状态
     *
     * @param cardId
     * @return
     */
    @RequestMapping(value = "/edit/card/enable", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult editCardEnable(@RequestParam Integer cardId) {
        if (cardService.updateEnable(cardId)) {
            return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
        }
        return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
    }

    /**
     * 修改注销状态
     *
     * @param cardId
     * @return
     */
    @RequestMapping(value = "/delete/card", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult editCardDeleteStatus(@RequestParam Integer cardId) {
        if (cardService.updateDeleteStatus(cardId)) {
            return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
        }
        return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
    }

    /**
     * 跳转到基站列表页面
     *
     * @return
     */
    @RequestMapping("/base/station/list")
    public String baseStationList(@ModelAttribute("condition") BaseStationCondition condition,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageIndex, Model model) {
        Page<BaseStationPO> page = baseStationService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", page.getTotalElements());
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("baseStations", page.getContent());
        model.addAttribute("onlineStatus", condition.getOnlineStatus());
        model.addAttribute("status", condition.getStatus());
        model.addAttribute("voltageStatus", condition.getVoltageStatus());
        model.addAttribute("dismantleStatus", condition.getDismantleStatus());
        return "equipment/base_station_list";
    }

    /**
     * 新增/修改基站信息
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/ajax/save/base/station", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveBaseStation(BaseStationSaveDTO dto) {
        BaseStationPO baseStationPO = baseStationService.save(dto);
        if (baseStationPO == null) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, dto);
    }

    /**
     * 修改基站启动状态
     *
     * @param baseStationId
     * @return
     */
    @RequestMapping(value = "/edit/base/station/enable", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult editBaseStationEnable(@RequestParam Integer baseStationId) {
        if (baseStationService.updateEnable(baseStationId)) {
            return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
        }
        return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
    }

    /**
     * 修改基站防拆卸状态
     *
     * @param baseStationId
     * @return
     */
    @RequestMapping(value = "/edit/base/station/dismantle", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult editBaseStationDismantle(@RequestParam Integer baseStationId) {
        if (baseStationService.updateDismantleStatus(baseStationId)) {
            return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
        }
        return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
    }

    /**
     * 跳转到定位器列表页面
     *
     * @return
     */
    @RequestMapping("/locator/list")
    public String locatorList(@ModelAttribute("condition") LocatorCondition condition,
                              @RequestParam(required = false, defaultValue = "1") Integer pageIndex, Model model) {
        condition.setBindingBaseStation(false);
        Page<LocatorPO> page = locatorService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", page.getTotalElements());
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("locators", page.getContent());
        model.addAttribute("onlineStatus", condition.getOnlineStatus());
        model.addAttribute("status", condition.getStatus());
        model.addAttribute("voltageStatus", condition.getVoltageStatus());
        model.addAttribute("dismantleStatus", condition.getDismantleStatus());
        model.addAttribute("areaList", locatorAreaService.findAll());

        return "equipment/locator_list";
    }

    @PostMapping("/locator/addToArea")
    @ResponseBody
    public ApiResult<Void> addToArea(int areaId, String[] ids) {
        locatorService.addToArea(areaId, ids);

        return ApiResult.success();
    }

    /**
     * 新增/修改定位器信息
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/ajax/save/locator", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveLocator(LocatorSaveDTO dto) {
        LocatorPO locatorPO = locatorService.save(dto);
        if (locatorPO == null) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, dto);
    }

    /**
     * 修改定位器启动状态
     *
     * @param locatorId
     * @return
     */
    @RequestMapping(value = "/edit/locator/enable", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult editLocatorEnable(@RequestParam Integer locatorId) {
        if (locatorService.updateEnable(locatorId)) {
            return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
        }
        return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
    }

    /**
     * 修改定位器防拆卸状态
     *
     * @param locatorId
     * @return
     */
    @RequestMapping(value = "/edit/locator/dismantle", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult editLocatorDismantle(@RequestParam Integer locatorId) {
        if (locatorService.updateDismantleStatus(locatorId)) {
            return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
        }
        return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
    }

    /**
     * 修改定位器是否在地图上展示
     *
     * @param locatorId
     * @return
     */
    @RequestMapping(value = "/edit/locator/show", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult editLocatorShow(@RequestParam Integer locatorId) {
        if (locatorService.updateShow(locatorId)) {
            return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
        }
        return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
    }

    /**
     * 根据楼层id获取该楼层的所有定位器
     *
     * @param strId
     * @return
     */
    @RequestMapping(value = "/locator/strId", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult locatorList(Integer strId, Boolean show) {
        List<LocatorVO> list = locatorService.findLocatorVoByStrId(strId, show);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, list);
    }

    @RequestMapping(value = "/save/location", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveLocation(LocatorSaveDTO dto) {
        LocatorVO locatorPO = locatorService.updateLocation(dto);
        if (locatorPO == null) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, locatorPO);
    }

    /**
     * 新增隐藏点
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/save/conceal/point", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveConcealPoint(ConcealPointSaveDTO dto) {
        ConcealPointPO concealPointPO = concealPointService.save(dto);
        if (concealPointPO == null) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, concealPointPO);
    }

    /**
     * 根据楼层id获取该楼层的所有定位器
     *
     * @param strId
     * @return
     */
    @RequestMapping(value = "/conceal/point/strId", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult concealPointList(Integer strId) {
        List<ConcealPointVO> list = concealPointService.findByStrId(strId);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, list);
    }

    @RequestMapping(value = "/update/conceal/point", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateConcealPoint(ConcealPointSaveDTO dto) {
        ConcealPointPO concealPointPO = concealPointService.update(dto);
        if (concealPointPO == null) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, concealPointPO);
    }

    @RequestMapping(value = "/delete/conceal/point", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteConcealPoint(Integer id) {
        boolean re = concealPointService.delete(id);
        if (!re) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    /**
     * 导入定位卡
     *
     * @param updateFile
     * @return
     */
    @ResponseBody
    @RequestMapping("/lead/card")
    public ApiResult leadCard(MultipartFile updateFile) {
        try {
            cardService.lead(updateFile);
        } catch (IOException e) {
            return ApiResult.errorWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, 1);
    }

    /**
     * 导入定位器
     *
     * @param updateFile
     * @return
     */
    @ResponseBody
    @RequestMapping("/lead/locator")
    public ApiResult leadLocator(MultipartFile updateFile) {
        try {
            locatorService.lead(updateFile);
        } catch (IOException e) {
            return ApiResult.errorWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, 1);
    }

    /**
     * 导入定位器
     *
     * @param updateFile
     * @return
     */
    @ResponseBody
    @RequestMapping("/lead/base/station")
    public ApiResult leadBaseStation(MultipartFile updateFile) {
        try {
            baseStationService.lead(updateFile);
        } catch (IOException e) {
            return ApiResult.errorWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, 1);
    }

    /**
     * 删除基站
     *
     * @param no
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete/base/station")
    public ApiResult deleteBaseStation(Long no) {
        try {
            baseStationService.deleteByNo(no);
        } catch (Exception e) {
            return ApiResult.errorWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, 1);
    }

    /**
     * 删除定位器
     *
     * @param no
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete/locator")
    public ApiResult deleteLocator(Long no) {
        try {
            locatorService.deleteByNo(no);
        } catch (Exception e) {
            return ApiResult.errorWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, 1);
    }

    /**
     * 统计基站
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/count/base/station")
    public ApiResult countBaseStation() {
        Map<String, Long> map;
        try {
            map = baseStationService.count();
        } catch (Exception e) {
            return ApiResult.errorWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, map);
    }
}
